package br.com.quad.managedBean.convert;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import br.com.quad.bean.TipoServico;

import br.com.quad.dao.DAOFactory;

@FacesConverter(value="converteTipoServico", forClass=TipoServico.class)
public class ConverteTipoServico implements Converter{

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		if(arg2.equals("0")){
			return new TipoServico();
		} else {
			TipoServico tipoServico = DAOFactory.getTipoServicoDAO().getTipoServico(Integer.valueOf(arg2));
			return tipoServico.retornaClone();
		}
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		return String.valueOf(((TipoServico) arg2).getCodigoTipoServico());
	}
}
